/**
 */

package com.bei_en.common.util.tencent;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;
import com.bei_en.common.util.GenerateUtil;
import com.qcloud.PicCloud;
import com.qcloud.PornDetectInfo;
import com.qcloud.UploadResult;

/**
 * @author Guocg
 *
 */
public class UploadUtil {

	private static String localPath = "upload/";

	private static int APP_ID = 10009107;
	private static String SECRET_ID = "AKIDKZZhlzO2WUeYw2JAlYPnZ1Fsv13h4ZA9";
	private static String SECRET_KEY = "BJhFfG6nlBulNCFvU6WOChVIxyipGN8J";
	private static String Bucket = "epmscos3";
	private static String real = "http://epmscos3-10009107.image.myqcloud.com/";
	private static String cname = "http://img.gxydwy.com/";

	static PicCloud pc = new PicCloud(APP_ID, SECRET_ID, SECRET_KEY, Bucket);

	public static String upload(MultipartFile file, boolean full)
			throws IOException {

		String version = pc.getVersion();
		// System.out.println(version);

		// String fileName =
		// "D:/amIurs/git/smart-yuda-java/SmartYuda/WebRoot/styles/img/logo.jpg";
		UploadResult ret = pc.upload(new BufferedInputStream(
				new ByteArrayInputStream(file.getBytes())));
		if (ret == null)
			return null;
		ret.print();
		// PornDetectInfo是黄图识别的返回结果
		/*
		 * PornDetectInfo info = pc.pornDetect(cname+ret.fileId); if
		 * (info!=null) { info.print(); if (info.result==1) return "sexy"; }
		 */
		return full ? cname + ret.fileId : ret.fileId;
	}

	/**
	 * @author Guocg
	 * @param request
	 * @param file
	 * @return
	 */
	public static String uploadLocal(HttpServletRequest request,
			MultipartFile file) throws IOException {

		String targetPath = request.getSession().getServletContext()
				.getRealPath("/");
		targetPath = StringUtils.replace(targetPath, "\\", "/");
		byte[] bytes = file.getBytes();
		String url = targetPath + localPath + GenerateUtil.geneCode() + ".xls";
		File file2 = new File(url);
		FileCopyUtils.copy(bytes, file2);
		return url;
	}
}
